package at.nebbl.openscotty;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

/**
 *
 * @author alex.nebel
 */
public class OpenScottyStart extends Activity {

    public EditText start;
    public EditText destination;
    public Button search;
    public TextView content;
    private clicker click;


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        // ToDo add your GUI initialization code here
        click = new clicker();
        viewSearch();
    }

    protected void viewSearch(){
        setContentView(R.layout.searchtest);
        start = (EditText) findViewById(R.id.start);
        destination = (EditText) findViewById(R.id.destination);
        search = (Button) findViewById(R.id.search);
        //address.setText("http://fahrplan.oebb.at/bin/query.exe/dn?S=Wien%2FSpittelau&Z=Langenlebarn&start=1&time=12%3A00&ld=oebb&REQ0JourneyDate=25.10.2009&REQ0JourneyProduct_list=0%3A1111111111010000-000000.");

        search.setOnClickListener(click);
    }

    protected void viewRoutes(){
        setContentView(R.layout.routes);
        content = (TextView) findViewById(R.id.content);
    }
    
    public StringBuilder GetText(String url) {
        HttpClient client = new DefaultHttpClient();
        HttpGet request = new HttpGet(url);
        try {
            HttpResponse response = client.execute(request);
            return GetText(response);
        } catch (Exception ex) {
            StringBuilder sb = new StringBuilder();
            sb.append(ex.getClass().toString() + "\n");
            sb.append(ex.getMessage());
            return sb;
        }
    }

    public static StringBuilder GetText(InputStream in) {
        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
        StringBuilder sb = new StringBuilder();
        String line = null;
        try {
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
        } catch (Exception ex) {
        } finally {
            try {

                in.close();
            } catch (Exception ex) {
            }
        }
        return sb;
    }

    public static StringBuilder GetText(HttpResponse response) {
        StringBuilder text;
        try {
            text = GetText(response.getEntity().getContent());
        } catch (Exception ex) {
            text = new StringBuilder(ex.getMessage());
        }
        return text;
    }


    private View parseResponse(StringBuilder response){
        
        return new View(this);
    }

    class clicker implements View.OnClickListener{

        public void onClick(View v) {
                TrainRide test = new TrainRide(new Station(start.getText().toString(), "S40"), new Station(destination.getText().toString(), "S40"));
                viewRoutes();
                
                //StringBuilder sb = GetText(test.getTrainRideQuery());



                /*int a = sb.indexOf("<td headers=\"hafasOVStop\"");
                sb.delete(0, a);
                a = sb.indexOf("<td headers=\"hafasOVStop\"", 10);
                sb.delete(a, sb.length());
                */
                ParseInterface pr = new ParseResponse();
                try{
                    //Route[] routes = pr.getRoutes(sb);
                    //content.setText(routes[0].toString()+routes[1].toString()+routes[2].toString());

                    RequestHandler req = new RequestHandler();
                    content.setText(req.requestStation("Tulln/Donau"));
                }catch(Exception ex){
                    content.setText(ex.toString());
                }
                
            }
    }
}